/* hal_uart.h */
#ifndef __HAL_UART_H__
#define __HAL_UART_H__

#include "hal_common.h"

/* flags. */
#define UART_FLAG_RX_REQ     UART_INTF_RXIF_MASK  /* rxfifo available for read data. */
#define UART_FLAG_TX_REQ     UART_INTF_TXIF_MASK  /* txfifo available for write data. */
#define UART_FLAG_TX_DONE    UART_INTF_TCIF_MASK  /* tx done and idle. */
#define UART_FLAG_RX_PAIRYT_ERR UART_INTF_PARIF_MASK /* rx parity error. */
#define UART_FLAG_RX_IDLE    UART_INTF_IDLEIF_MASK   /* rx detects an idle. */
#define UART_FLAG_RX_FRAME_ERR  UART_INTF_FEIF_MASK   /* rx frame error. */
#define UART_FLAG_RX_FIFO_OVERRUN   UART_INTF_OVRIF_MASK
#define UART_FLAG_RX_NOISE   UART_INTF_NOZIF_MASK
#define UART_FLAG_RX_MATCH   UART_INTF_MATIF_MASK
#define UART_FLAG_RX_EDGE    UART_INTF_RXEDGEIF_MASK
#define UART_FLAG_RX_LIN_BREAK UART_INTF_LBKDIF_MASK
#define UART_FLAG_TX_FIFO_OVERFLOW  UART_INTF_TFEIF_MASK
#define UART_FLAG_RX_FIFO_UNDERFLOW UART_INTF_RFEIF_MASK
#define UART_FLAG_RX_BUSY    UART_INTF_RXBUSY_MASK /* rx line is active. */

typedef enum
{
    UART_Parity_None = 0u,
    UART_Parity_Even = 2u,
    UART_Parity_Odd = 3u,
} UART_Parity_Type;

typedef enum
{
    UART_DataBits_7b = 0u,
    UART_DataBits_8b = 1u,
    UART_DataBits_9b = 2u,
    UART_DataBits_10b= 3u,
} UART_DataBits_Type;

typedef enum
{
    UART_StopBits_1b = 0u,
    UART_StopBits_2b = 1u,
} UART_StopBits_Type;

typedef enum
{
    UART_IdleSize_1B  = 0u,
    UART_IdleSize_2B  = 1u,
    UART_IdleSize_4B  = 2u,
    UART_IdleSize_8B  = 3u,
    UART_IdleSize_16B = 4u,
    UART_IdleSize_32B = 5u,
    UART_IdleSize_64B = 6u,
    UART_IdleSize_128B= 7u,
} UART_IdleSize_Type;

typedef struct
{
    uint32_t SrcClkFreq;
    uint32_t Baudrate;
    
    UART_DataBits_Type DataBits;
    UART_StopBits_Type StopBits;
    UART_IdleSize_Type IdleSize;
    
    UART_Parity_Type Parity;
    
    uint32_t TxFifoWatermark; /* 0 - 7. */
    uint32_t RxFifoWatermark; /* 0 - 7. */
    
} UART_Init_Type;

void UART_Init(UART_Type * base, UART_Init_Type * init);

uint32_t UART_GetStatusFlags(UART_Type * base);
void UART_ClearStatusFlags(UART_Type * base, uint32_t flags);
void UART_EnableInterrupts(UART_Type * base, uint32_t flags);
void UART_DisableInterrupts(UART_Type * base, uint32_t flags);
uint32_t UART_GetEnabledInterrupts(UART_Type * base);

void UART_PutTxFifoData(UART_Type * base, uint8_t dat);
uint8_t UART_GetRxFifoData(UART_Type * base);

void UART_EnableRxDma(UART_Type * base, bool enable);
void UART_EnableTxDma(UART_Type * base, bool enable);


#endif /* __HAL_UART_H__ */

